package space.xxhui.javatts.tts.engine.impl;

import com.spreada.utils.chinese.ZHConverter;
import org.ansj.domain.Result;
import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.BaseAnalysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import space.xxhui.javatts.tts.engine.Engine;
import space.xxhui.javatts.tts.reader.DictReader;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HitvzByHui
 * date 2021-04-05
 * desc 粤语转换实现
 */
@Component("cantoneseConvertEngine")
public class CantoneseConvertEngine implements Engine {

    @Autowired
    private DictReader dictReader;

    private boolean isSpokenLanguage = false;//是否口语

    private ZHConverter zhConverter = ZHConverter.getInstance(ZHConverter.SIMPLIFIED);

    public String convert(String text) {
        dictReader.loadDict();
        text = zhConverter.convert(text);//繁体转换为简体
        List<String> asciiSegmentList = getAsciiSegment(text);
        if (asciiSegmentList.size() > 0) {
            for (String asciiSegment : asciiSegmentList) {

            }
        }
        return text;
    }


    public List<String> getAsciiSegment(String text) {
        Result segmentResult = BaseAnalysis.parse(text);
        List<String> result = new ArrayList<>(segmentResult.size());
        for (Term item : segmentResult.getTerms()) {
            String word = item.getName();
            char[] term = word.toCharArray();
            String asciiWordSegment = "";
            for (int i = 0; i < term.length; i++) {
                asciiWordSegment += (int) term[i] + "|";
            }
            result.add(asciiWordSegment);
        }
        return result;
    }

    public static void main(String[] args) {
        Result result = BaseAnalysis.parse("可以卧，个发音");
        System.out.println();
    }


}
